import Vue from 'vue'
const config = require('@/config')
import {ADMIN} from '@/config/default'
import {formatFullPath} from '@/utils/i18n'
import {filterMenu} from '@/utils/authority-utils'
import {getLocalSetting} from '@/utils/themeUtil'
import { DEFAULT_THEME,DEFAULT_COLOR_WEAK,DEFAULT_LANG,DEFAULT_ANIMATE,DEFAULT_MULTI_PAGE,DEFAULT_LAYOUT_MODE,DEFAULT_COLOR,DEFAULT_FIXED_HEADER,DEFAULT_FIXED_SIDEMENU,DEFAULT_CONTENT_WIDTH_TYPE,DEFAULT_HIDDEN_SETTING} from "@/store/mutation-types"

const localSetting = getLocalSetting(true)

export default {
  namespaced: true,
  state: {
    isMobile: false,
    animates: ADMIN.animates,
    palettes: ADMIN.palettes,
    pageMinHeight: 0,
    menuData: [],
    activatedFirst: undefined,
    ...config,
    ...localSetting
  },
  getters: {
    menuData(state, getters, rootState) {
      if (state.filterMenu) {
        const {permissions, roles} = rootState.account
        filterMenu(state.menuData, permissions, roles)
      }
      return state.menuData
    },
    firstMenu(state) {
      const {menuData} = state
      if (menuData.length > 0 && !menuData[0].fullPath) {
        formatFullPath(menuData)
      }
      return menuData.map(item => {
        const menuItem = {...item}
        delete menuItem.children
        return menuItem
      })
    },
    subMenu(state) {
      const {menuData, activatedFirst} = state
      if (!menuData[0].path) {
        formatFullPath(menuData)
      }
      const current = menuData.find(menu => menu.path === activatedFirst)
      return current && current.children ? current.children : []
    }
  },
  mutations: {
    setDevice (state, isMobile) {
      state.isMobile = isMobile
    },
    setTheme (state, theme) {
      state.theme = theme
      Vue.ls.set(DEFAULT_THEME, theme)   
    },
    setColor (state, color) {
      const data ={...state.theme}
      data.color=color
      state.theme = data
      Vue.ls.set(DEFAULT_THEME, data)   
      Vue.ls.set(DEFAULT_COLOR, color)  
    },
    setLayout (state, layout) {
      state.layout = layout
      Vue.ls.set(DEFAULT_LAYOUT_MODE, layout)  
    },
    setMultiPage (state, multiPage) {
      state.multiPage = multiPage
      Vue.ls.set(DEFAULT_MULTI_PAGE, multiPage)  
    },
    setAnimate (state, animate) {
      state.animate = animate
      Vue.ls.set(DEFAULT_ANIMATE, animate)  
    },
    setWeekMode(state, weekMode) {
      state.weekMode = weekMode
      Vue.ls.set(DEFAULT_COLOR_WEAK, weekMode)  
    },
    setFixedHeader(state, fixedHeader) {
      state.fixedHeader = fixedHeader
      Vue.ls.set(DEFAULT_FIXED_HEADER, fixedHeader)  
    },
    setFixedSideBar(state, fixedSideBar) {
      state.fixedSideBar = fixedSideBar
      Vue.ls.set(DEFAULT_FIXED_SIDEMENU, fixedSideBar)  
    },
    setLang(state, lang) {
      state.lang = lang
      Vue.ls.set(DEFAULT_LANG, lang)  
    },
    setHideSetting(state, hideSetting) {
      state.hideSetting = hideSetting
      Vue.ls.set(DEFAULT_HIDDEN_SETTING, hideSetting)  
    },
    correctPageMinHeight(state, minHeight) {
      state.pageMinHeight += minHeight
    },
    setMenuData(state, menuData) {
      state.menuData = menuData
    },
    setAsyncRoutes(state, asyncRoutes) {
      state.asyncRoutes = asyncRoutes
    },
    setPageWidth(state, pageWidth) {
      state.pageWidth = pageWidth
      Vue.ls.set(DEFAULT_CONTENT_WIDTH_TYPE, pageWidth)   
    },
    setActivatedFirst(state, activatedFirst) {
      state.activatedFirst = activatedFirst
    },
    setFixedTabs(state, fixedTabs) {
      state.fixedTabs = fixedTabs
    }
  }
}
